package com.example.newsdaily.slice;

import com.alibaba.fastjson.JSON;
import com.example.newsdaily.ResourceTable;
import com.example.newsdaily.entity.TUser;
import com.example.newsdaily.utl.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.data.preferences.Preferences;

public class ChangeCodeAbilitySlice extends AbilitySlice {
    Button button;
    TextField tf_password;
    TextField tf_password2;
    Preferences preferences= LocalDataUtil.preferences;

    String name=LocalDataUtil.preferences.getString(ContainUtil.USER_NAME_KEY,"");
    Long id=preferences.getLong(ContainUtil.USER_ID_KEY,0);
    ProgressDialogUtil progressDialogUtil=new ProgressDialogUtil(this);
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_ChangeCode);
        tf_password=(TextField)findComponentById(ResourceTable.Id_rst_password);
        tf_password2=(TextField)findComponentById(ResourceTable.Id_rst_password2);
        button=(Button)findComponentById(ResourceTable.Id_rst_conf);

        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                reset();
            }
        });

    }
    public void reset(){
        String password=tf_password.getText();
        String password2=tf_password2.getText();
        if(password==null){
            ToastUtil.makeToast(getContext(), "密码不能为空", ToastUtil.TOAST_LONG);
            return;
        }else if(password2==null){
            ToastUtil.makeToast(getContext(), "请再输入一次密码", ToastUtil.TOAST_LONG);
            return;
        }
        if(!password.equals(password2)){
            ToastUtil.makeToast(getContext(),"两次输入的密码不一致",ToastUtil.TOAST_LONG);
            return;
        }
        progressDialogUtil.showProgress(true);

        new Thread(new Runnable() {
            @Override
            public void run() {
                String result= HttpClientUtil.doGet(ContainUtil.Change_USERCODE_URL+
                        "?id="+ id+ "&repassword="+password+"&name="+name);
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        progressDialogUtil.showProgress(false);
                        TUser user = null;
                        try {
                            user = JSON.parseObject(result, TUser.class);
                        } catch (Exception e) {
                        }
                        if (user != null) {
                            ToastUtil.makeToast(getContext(), "更改成功", ToastUtil.TOAST_LONG);
                            terminateAbility();
                            return;
                        } else {
                            ToastUtil.makeToast(getContext(), "更改失败", ToastUtil.TOAST_LONG);
                        }
                    }
                });
            }
        }).start();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
